/*
 * File Name:               XPropertyBinder.cs
 *
 * Description:             属性绑定接口的实现类
 * Author:                  lisiyu
 * Create Date:             2016/06/27
 */

using System;

public class XPropertyBinder<T> : IPropertybinder
{
    public XProperty<T> mProperty;
    public Action<T> mCallback;

    public struct Argument
    {
        public XProperty<T> property;
        public Action<T> callback;
    }

    public void onSpawn(object args)
    {
        var arg = (Argument)args;
        mProperty = arg.property;
        mCallback = arg.callback;

        //mProperty = args[0] as XProperty<T>;
        //mCallback = args[1] as Action<T>;
    }

    public void onRecycle()
    {
        unbind();
        mProperty = null;
        mCallback = null;
    }

    public void bind(bool callOnFirst = true)
    {
        if (mProperty == null || mCallback == null) return;

        mProperty.addListener(mCallback);

        if (callOnFirst)
            mCallback(mProperty.getProperty());
    }

    private void unbind()
    {
        if (mProperty == null || mCallback == null) return;

        mProperty.removeListener(mCallback);
    }
}